﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Http.Headers;
using System.Text;

namespace XCrawler
{
    /// <summary>
    /// 爬虫解析信息
    /// </summary>
    public class ParserBag
    {

        #region "属性集合"

        private Hashtable BagCollection = new Hashtable();

        #endregion

        #region "属性索引"
        /// <summary>
        /// 属性索引
        /// </summary>
        /// <param name="name">属性名</param>
        /// <returns></returns>
        public object this[string name]
        {
            get
            {
                return BagCollection[name];
            }
            set
            {
                BagCollection[name] = value;
            }
        }

        /// <summary>
        /// 获取属性
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public T GetValue<T>(string key, T value)
        {
            return BagCollection.GetValue<T>(key, value);
        }

        /// <summary>
        /// 设置属性
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        public void SetValue(string key, object value)
        {
            BagCollection.SetValue(key, value);
        }

        #endregion



        #region "属性"
        /// <summary>
        /// 返回流
        /// </summary>
        public Stream ResponseBuffer
        {
            get { return GetValue("ResponseBuffer", new MemoryStream()); }
            internal set { SetValue("ResponseBuffer", value); }
        }

        /// <summary>
        /// 线程编号
        /// </summary>
        public int ThreadId
        {
            get { return GetValue("ThreadId", System.Threading.Thread.CurrentThread.ManagedThreadId); }
            internal set { SetValue("ThreadId", value); }
        }

        /// <summary>
        /// 计时器
        /// </summary>
        public Stopwatch WatchTimer { get; set; }

        /// <summary>
        /// 耗时
        /// </summary>
        public long DownloadTime
        {
            get { return GetValue("DownloadTime", 0); }
            internal set { SetValue("DownloadTime", value); }
        }

        /// <summary>
        /// 开始事件
        /// </summary>
        public DateTime StartTime
        {
            get { return GetValue("StartTime", default(DateTime)); }
            internal set { SetValue("StartTime", value); }
        }
        /// <summary>
        /// 结束事件
        /// </summary>
        public DateTime EndTime
        {
            get { return GetValue("EndTime", default(DateTime)); }
            internal set { SetValue("EndTime", value); }
        }
 
        /// <summary>
        /// 响应头
        /// </summary>
        public HttpContentHeaders ResponseHeaders
        {
            get { return GetValue("ResponseHeaders", default(HttpContentHeaders)); }
            internal set { SetValue("ResponseHeaders", value); }
        }

        /// <summary>
        /// 请求头
        /// </summary>
        public HttpRequestHeaders RequestHeaders
        {
            get { return GetValue("RequestHeaders", default(HttpRequestHeaders)); }
            internal set { SetValue("RequestHeaders", value); }
        }

        /// <summary>
        /// 响应状态码
        /// </summary>
        public HttpStatusCode StatusCode
        {
            get { return GetValue("StatusCode", HttpStatusCode.Continue); }
            internal set { SetValue("StatusCode", value); }
        }

        /// <summary>
        /// 响应内容类型
        /// </summary>
        public string ContentType
        {
            get { return GetValue("ContentType", string.Empty); }
            internal set { SetValue("ContentType", value); }
        }

        /// <summary>
        /// 响应内容编码
        /// </summary>
        public string ContentEncoding
        {
            get { return GetValue("ContentEncoding", string.Empty); }
            internal set { SetValue("ContentEncoding", value); }
        }

        /// <summary>
        /// 响应内容字符编码
        /// </summary>
        public string ContentCharset
        {
            get { return GetValue("ContentCharset", "ASCII"); }
            internal set { SetValue("ContentCharset", value); }
        }




        public string SourceText
        {
            get { return GetValue("SourceText", string.Empty); }
            internal set { SetValue("SourceText", value); }
        }



        /// <summary>
        /// 爬行地址
        /// </summary>
        public Uri CrawlerUrl
        {
            get { return GetValue("CrawlerUrl", default(Uri)); }
            internal set { SetValue("CrawlerUrl", value); }
        }

        /// <summary>
        /// 是否成功
        /// </summary>
        public bool Success
        {
            get { return GetValue("Success", false); }
            internal set { SetValue("Success", value); }
        }
        

        #endregion


        #region "方法"

        /// <summary>
        /// 停止计时
        /// </summary>
        /// <returns></returns>
        public long StopWatchTimer()
        {
            EndTime = DateTime.Now;
            WatchTimer.Stop();
            DownloadTime = WatchTimer.ElapsedMilliseconds;//获取请求执行时间

            return DownloadTime;
        }

        /// <summary>
        /// 开始计时
        /// </summary>
        /// <returns></returns>
        public void StartWatchTimer()
        {
            WatchTimer = new Stopwatch();
            WatchTimer.Start();
            StartTime = DateTime.Now;
        }

        #endregion

    }
}
